//
//  WDImageUtils.m
//  MyDemos
//
//  Created by iMac on 2018/3/12.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDImageUtils.h"

@implementation WDImageUtils


/// 得到一个带边框的圆形 要求1.显示到控件上的目标 size 必须为正方形 | 2.知道将要显示在控件上的大小和将要显示到空间上的边框的宽度 ->图片会先剪裁成一张从中间部分获取的最大的正方形, 在合并为目标图像
+ (UIImage *)imageClipToCircle:(UIImage *)image targetImageShowSize:(CGSize)targetShowSize withBorderWidth:(CGFloat)width borderColor:(UIColor *)color {
    image = [self imageClipToCircle:image];
    CGFloat scale = image.size.width / (targetShowSize.width - width);
    CGSize borderSize = CGSizeMake(targetShowSize.width * scale, targetShowSize.height * scale);
    UIImage *borderImage = [self imageWithColor:color size:borderSize];
    borderImage = [self circleImage:borderImage withParam:0];
    return [self addImage:borderImage withImage:image];
}
/// 得到一张纯色图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef contex = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contex, color.CGColor);
    CGContextFillRect(contex, CGRectMake(0, 0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


/// 图片剪裁成圆形
+ (UIImage *)imageClipToCircle:(UIImage *)image {
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat margin = MIN(width, height) / 2.f;
    CGFloat x = ABS(width / 2.f - margin);
    CGFloat y = ABS(height / 2.f - margin);
    CGRect rect = CGRectMake(x, y, MIN(height, width), MIN(height, width));
    image = [self imageClipToSquare:image imageRect:rect];
    CGFloat radius = image.size.width / 2.f;
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    CGContextRef contex = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(contex, YES);
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    [path closePath];
    [path addClip];
    [image drawInRect:CGRectMake(0, 0, radius * 2, radius * 2)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
/// 截取 rect 大小的图片
+ (UIImage *)imageClipToSquare:(UIImage *)image imageRect:(CGRect)rect {
    CGImageRef imageRef = image.CGImage;
    CGImageRef newImageRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    return newImage;
}

/// 合并两张图片
+ (UIImage *)addImage:(UIImage *)image1 withImage:(UIImage *)image2 {
    UIGraphicsBeginImageContext(image1.size);
    
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
    
    [image2 drawInRect:CGRectMake((image1.size.width - image2.size.width)/2,(image1.size.height - image2.size.height)/2, image2.size.width, image2.size.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}



//// ----代码存储
/// 从网上抄的, 获取圆形图片, 但是感觉包含了图片两边多余的透明部分, 图片加透明部分不是正方形, 放到正方形视图中会导致变形
+ (UIImage*)circleImage:(UIImage*) image withParam:(CGFloat) inset {
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    CGContextSetLineWidth(context, 0);
    //    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat margin = MIN(width, height) / 2.f;
    CGFloat x = ABS(width / 2.f - margin);
    CGFloat y = ABS(height / 2.f - margin);
    CGRect rect = CGRectMake(x, y, MIN(height, width), MIN(height, width));
    CGContextAddEllipseInRect(context, rect);
    rect = CGRectMake(0, 0, width, height);
    CGContextClip(context);
    
    [image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
    CGContextStrokePath(context);
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}
@end
